package com.windoent.yugioh.ui.activity.some.photoviewpager;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.baidu.mobstat.StatService;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.multi.FeedbackUpLoadImageBean;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.ImageUtil;
import com.windoent.yugioh.utils.SaveBitmapGalleryUtils;
import com.windoent.yugioh.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * author : HP
 * date   : 2020-08-12 11:52
 * desc   :相册选择器
 */
public class PhotoViewPagerActivity extends BaseActivity implements View.OnClickListener {

    private PhotoViewPager mViewPager;
    private int currentPosition;
    private PhotoViewPagerAdapter adapter;
    private TextView mTvImageCount;
    private ImageView mTvSaveImage;
    private List<String> Urls;


    @Override
    public int getLayout() {
        return R.layout.activity_photo_view;
    }

    public void initView() {
        immersiveStatusBar();
        mViewPager = (PhotoViewPager) findViewById(R.id.view_pager_photo);
        mTvImageCount = (TextView) findViewById(R.id.tv_image_count);
        mTvSaveImage = (ImageView) findViewById(R.id.iv_save_pic);
        mTvSaveImage.setOnClickListener(this);

    }

    public void initData() {

        Intent intent = getIntent();
        currentPosition = intent.getIntExtra("currentPosition", 0);
        List<FeedbackUpLoadImageBean> dataBean = ((List<FeedbackUpLoadImageBean>) intent.getSerializableExtra("pagerviewlistdataBean"));
        Urls = getUrlList(dataBean);

        adapter = new PhotoViewPagerAdapter(Urls, this);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(currentPosition, false);
        mTvImageCount.setText(currentPosition + 1 + "/" + Urls.size());
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentPosition = position;
                mTvImageCount.setText(currentPosition + 1 + "/" + Urls.size());
            }
        });
    }

    private List<String> getUrlList(List<FeedbackUpLoadImageBean> dataBean) {
        List<String> arrayList = new ArrayList<>();
        arrayList.clear();
        if (dataBean != null && dataBean.size() > 0) {
            for (int i = 0; i < dataBean.size(); i++) {
                FeedbackUpLoadImageBean bean = dataBean.get(i);
                if (BaseMethod.notNull(bean.getUrl())) {
                    arrayList.add(bean.getUrl());
                }
            }
            return arrayList;
        }
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_save_pic:
                //save image
                int currentItem = mViewPager.getCurrentItem();
                if (Urls != null && Urls.size() >= currentItem)
                    rxPermission(Urls.get(currentItem));
                break;
        }
    }

    //权限申请
    @SuppressLint("CheckResult")
    private void rxPermission(String h5url) {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            if (!TextUtils.isEmpty(h5url)) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        SaveBitmapGalleryUtils utils = new SaveBitmapGalleryUtils();
                                        Bitmap bitmap = ImageUtil.getUrlBitMap(h5url);
                                        boolean save = utils.addJpgSignatureToGallery(PhotoViewPagerActivity.this, bitmap, "feiyu");
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                ToastUtils.showText(save ? getString(R.string.string_save_gallery_success) : getString(R.string.string_save_gallery_fail));
                                            }
                                        });
                                    }
                                }).start();
                            }
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "图片浏览界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "图片浏览界面");
    }
}
